package com.koron.css2.serviceManage.impl;

import com.koron.common.bean.OperationBean;
import com.koron.common.stub.Port;
import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.TreeBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.swan.bean.MessageBean;

import java.util.*;

public class ServiceModuleList implements ServerInterface {
    @Autowired
    public ApplicationConfig config;

    
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		MessageBean<HashMap> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", HashMap.class);

		try {
			HashMap<String,TreeBean> treeBeans = new HashMap<String,TreeBean>();
			info.setData(treeBeans);
			ArrayList<String> codes = (ArrayList<String>)((Map)req.getData()).get("systemCodes");
			for (String sysCode : codes) {
				if("001".equals(sysCode)||"003".equals(sysCode)||"002".equals(sysCode)) {//运维与分析系统，不做菜单权限控制，登陆即可用
					continue;
				}
				Port port = new Port(ApplicationConfig.appCustomerAppid, ApplicationConfig.appCustomerSecret);
				MessageBean<Map> mapMsg = port.getOperations(0l, 2, 0, -1, "");
				List<OperationBean> list =(List<OperationBean>)mapMsg.getData().get("list");
				
				//给操作设置父级id,方便生成树结果
				Stack<OperationBean> stack = new Stack<>();
				for (OperationBean operationTreeBean : list) {
					if (stack.isEmpty()) {
						stack.push(operationTreeBean);
					} else {
						OperationBean top = stack.peek();
						while (!stack.isEmpty()
								&& operationTreeBean.getParentMask().intValue() <= top.getParentMask().intValue()) {
							stack.pop();
							if (!stack.isEmpty())
								top = stack.peek();
						}
						operationTreeBean.setParentId(top.getId());
						stack.push(operationTreeBean);
					}
				}
				TreeBean treeBean = new TreeBean();
				for (OperationBean bean : list) {
					// 如果code是以父级开头，且长度多3位，说明这是他的下级
					if (bean.getParentId()==null|| bean.getParentId()==0) {
						// 创建根目录
						treeBean.setId(bean.getId()+"");
						treeBean.setCode(bean.getCode());
						treeBean.setName("根目录");
						treeBean.setParent("");
						treeBean.setIsParent(true);
					}
				}
				treeBeans.put(sysCode,treeBean);
				// 递归下级目录
				recTreeOp(list, treeBean);
			}
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}
	
	/**
	 * 递归查询下级目录树
	 * 
	 * @param list
	 * @param treeBean
	 */
	private void recTreeOp(List<OperationBean> list, TreeBean treeBean) {
		for (OperationBean bean : list) {
			// 如果code是以父级开头，且长度多3位，说明这是他的下级
			if (treeBean.getId().equals(bean.getParentId()==null?"":bean.getParentId()+"")) {
				TreeBean b = new TreeBean();
				b.setId(bean.getId() + "");
				b.setCode(bean.getCode());
				b.setName(bean.getName());
				b.setParent(treeBean.getId() + "");
				// 当前节点假设为叶子节点
				b.setIsLeaf(1);
				b.setIsParent(false);
				// 当前节点的上一节点设为不是叶子节点
				treeBean.setIsLeaf(0);
				treeBean.setIsParent(true);
				treeBean.getChildren().add(b);
				recTreeOp(list, b); // 递归循环下级目录
			}
		}
	}
}
